<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="试卷ID" prop="paperId">
      <el-input v-model="dataForm.paperId" placeholder="试卷ID"></el-input>
    </el-form-item>
    <el-form-item label="题目ID" prop="questionId">
      <el-input v-model="dataForm.questionId" placeholder="题目ID"></el-input>
    </el-form-item>
    <el-form-item label="大题序号" prop="sortNoBig">
      <el-input v-model="dataForm.sortNoBig" placeholder="大题序号"></el-input>
    </el-form-item>
    <el-form-item label="小题序号" prop="sortNoSmall">
      <el-input v-model="dataForm.sortNoSmall" placeholder="小题序号"></el-input>
    </el-form-item>
    <el-form-item label="分数" prop="score">
      <el-input v-model="dataForm.score" placeholder="分数"></el-input>
    </el-form-item>
    <el-form-item label="创建人" prop="createBy">
      <el-input v-model="dataForm.createBy" placeholder="创建人"></el-input>
    </el-form-item>
    <el-form-item label="修改人" prop="updateBy">
      <el-input v-model="dataForm.updateBy" placeholder="修改人"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createDate">
      <el-input v-model="dataForm.createDate" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateDate">
      <el-input v-model="dataForm.updateDate" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="是否删除(0代表否,1 代表是)" prop="isDeleted">
      <el-input v-model="dataForm.isDeleted" placeholder="是否删除(0代表否,1 代表是)"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          paperId: '',
          questionId: '',
          sortNoBig: '',
          sortNoSmall: '',
          score: '',
          createBy: '',
          updateBy: '',
          createDate: '',
          updateDate: '',
          isDeleted: ''
        },
        dataRule: {
          paperId: [
            { required: true, message: '试卷ID不能为空', trigger: 'blur' }
          ],
          questionId: [
            { required: true, message: '题目ID不能为空', trigger: 'blur' }
          ],
          sortNoBig: [
            { required: true, message: '大题序号不能为空', trigger: 'blur' }
          ],
          sortNoSmall: [
            { required: true, message: '小题序号不能为空', trigger: 'blur' }
          ],
          score: [
            { required: true, message: '分数不能为空', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          updateBy: [
            { required: true, message: '修改人不能为空', trigger: 'blur' }
          ],
          createDate: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateDate: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '是否删除(0代表否,1 代表是)不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/tiku/paperquestion/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.paperId = data.paperQuestion.paperId
                this.dataForm.questionId = data.paperQuestion.questionId
                this.dataForm.sortNoBig = data.paperQuestion.sortNoBig
                this.dataForm.sortNoSmall = data.paperQuestion.sortNoSmall
                this.dataForm.score = data.paperQuestion.score
                this.dataForm.createBy = data.paperQuestion.createBy
                this.dataForm.updateBy = data.paperQuestion.updateBy
                this.dataForm.createDate = data.paperQuestion.createDate
                this.dataForm.updateDate = data.paperQuestion.updateDate
                this.dataForm.isDeleted = data.paperQuestion.isDeleted
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/tiku/paperquestion/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'paperId': this.dataForm.paperId,
                'questionId': this.dataForm.questionId,
                'sortNoBig': this.dataForm.sortNoBig,
                'sortNoSmall': this.dataForm.sortNoSmall,
                'score': this.dataForm.score,
                'createBy': this.dataForm.createBy,
                'updateBy': this.dataForm.updateBy,
                'createDate': this.dataForm.createDate,
                'updateDate': this.dataForm.updateDate,
                'isDeleted': this.dataForm.isDeleted
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
